package jmine.tec.web.wicket.pages;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jmine.tec.web.wicket.component.paging.DAOQueryDataProvider;
import jmine.tec.web.wicket.component.paging.PagingNavigatorContainer;
import jmine.tec.web.wicket.spring.SpringBeanReference;
import junit.framework.Assert;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.DataGridView;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.persist.hibernate.page.PagingData;
import bancosys.tec.persist.hibernate.page.PagingService;

/**
 * @author takeshi
 */
public class Paging extends WebPage {

    private static final int PAGE_SIZE = 20;

    private static final int TAMANHO = 10000;

    private static final int MAX_PAGES = 6;

    /**
     * 
     */
    @SuppressWarnings("unchecked")
    public Paging() {
        final List<String> list = new ArrayList<String>();
        for (int i = 0; i < TAMANHO; i++) {
            list.add("result: " + i);
        }

        SpringBeanReference<BaseDAOFactory> baseDAOFactoryRef = SpringBeanReference.forName("daoFactory");
        final SpringBeanReference<PagingService> pagingServiceRef = SpringBeanReference.forName("pagingService");
        DAOQueryDataProvider<String> provider = new DAOQueryDataProvider<String>(PAGE_SIZE, baseDAOFactoryRef, pagingServiceRef) {

            public IModel<String> model(String object) {
                return new Model<String>(object);
            }

            @Override
            protected List<String> makeQuery(BaseDAOFactory daoFactory) {
                PagingData data = pagingServiceRef.get().getPagingData();
                Assert.assertNotNull(data);
                if (!data.isDataInitialized() || data.isIncomplete()) {
                    int maxResults = data.getItensPerPage() * MAX_PAGES;
                    int first = data.getItensPerPage() * data.getCurrentPage();
                    String[] results = new String[data.getItensPerPage()];
                    for (int i = 0; i < data.getItensPerPage(); i++) {
                        results[i] = list.get(i + first);
                    }
                    data.setTotalItens(Math.min(first + maxResults, list.size()));
                    data.setIncomplete(data.getTotalItens() == first + maxResults);
                    return Arrays.asList(results);
                }
                int startIndex = data.getCurrentPage() * data.getItensPerPage();
                return list.subList(startIndex, startIndex + data.getItensPerPage());
            }
        };

        DataGridView<String> grid = new DataGridView<String>("list", new ICellPopulator[]{ new ICellPopulator<String>() {

            public void populateItem(Item<ICellPopulator<String>> cellItem, String componentId, IModel<String> rowModel) {
                cellItem.add(new Label(componentId, rowModel.getObject()));
            }

            public void detach() {

            }
        } }, provider) {
            @Override
            public int getViewSize() {
                return PAGE_SIZE;
            }
        };
        provider.setPageable(grid);
        this.add(grid);
        grid.setRowsPerPage(PAGE_SIZE);
        this.add(new PagingNavigatorContainer("navigator", grid));
    }

}
